package com.quasar.hpatient.module.home_temperature;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.dialog.CalendarDialog;
import com.quasar.hpatient.dialog.InputDialog;
import com.quasar.hpatient.dialog.TimeDialog;
import com.quasar.hpatient.dialog.WarningDialog;
import com.quasar.hpatient.module.home_daily.HomeDailyActivity;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.context.BaseConstant;
import lib.quasar.widget.chart.ChartView;
import lib.quasar.widget.edit.AlterEditText;
import lib.quasar.widget.menu.NavigationView;

/**
 * description: 首页 ==> 体温
 * created by kalu on 2018/3/29 9:03
 */
public class TemperatureActivity extends BaseActivity<TemperaturePresenter> implements TemperatureView {

    public static final String MULIT = "MULIT";
    public static final String DATE = "DATE";

    @Override
    public void onBackPressed() {

        final int intExtra = getIntent().getIntExtra(HomeDailyActivity.POSITION, -1);
        final Intent result = getPresenter().getResult(this, intExtra);
        setResult(HomeDailyActivity.RESULT_CODE, result);

        super.onBackPressed();
    }

    @Override
    public int initView() {
        return R.layout.activity_home_temperature;
    }

    @Override
    public void initDataLocal() {

        final AlterEditText edit = findViewById(R.id.activity_home_temperature_input);
        edit.setNumberFilter(1, 3);
        edit.setOnInputChangeListener((input, isModelChange, isMax) -> {
            if (!isMax) return;
            toast("体温不能高于45°C, 请输入正确体温值");
        });

        final boolean mulit = getIntent().getBooleanExtra(MULIT, false);

        final NavigationView menu = findViewById(R.id.home_temperature_menu);
        menu.setMenuEnable(!mulit);
        if (mulit) {
            menu.setOnBackChangeListener(() -> onBackPressed());
        } else {
            menu.setOnNavigationChangeListener((isBack, isMenu) -> {
                if (isBack) onBackPressed();
                else if (isMenu) {
                    final RecyclerView recycler = findViewById(R.id.activity_home_temperature_list);
                    final ChartView chart = findViewById(R.id.activity_home_temperature_chart);
                    final EditText text = findViewById(R.id.activity_home_temperature_input);
                    getPresenter().saveTemperature(this, chart, recycler, text);
                }
            });
        }

        final RecyclerView recycler = findViewById(R.id.activity_home_temperature_list);
        getPresenter().initList(this, recycler, mulit);

        findViewById(R.id.home_temperature_date).setVisibility(mulit ? View.GONE : View.VISIBLE);
        findViewById(R.id.home_temperature_menu3).setVisibility(mulit ? View.GONE : View.VISIBLE);
        findViewById(R.id.home_temperature_menu4).setVisibility(mulit ? View.GONE : View.VISIBLE);
        if (!mulit) {
            findViewById(R.id.activity_home_temperature_input).requestFocus();
            findViewById(R.id.home_temperature_time).setOnClickListener(v -> showTime());
            final TextView dates = findViewById(R.id.home_temperature_date);
            final TextView times = findViewById(R.id.home_temperature_time);
            final String stringExtra = getIntent().getStringExtra(DATE);

            if (TextUtils.isEmpty(stringExtra)) {
                getPresenter().initTime(this, dates, times);
            } else {
                setText(dates, stringExtra);
                getPresenter().initTime(this, null, times);
            }

            dates.setOnClickListener(v -> {

                final String trim = dates.getText().toString().trim();
                final String[] split = trim.split("-");
                int year = Integer.parseInt(split[0]);
                int month;
                if (split[1].startsWith("0")) {
                    month = Integer.parseInt(String.valueOf(split[1].charAt(1)));
                } else {
                    month = Integer.parseInt(split[1]);
                }
                int day;
                if (split[2].startsWith("0")) {
                    day = Integer.parseInt(String.valueOf(split[2].charAt(1)));
                } else {
                    day = Integer.parseInt(split[2]);
                }
                showCalendar(dates, year, month, day);
            });
        } else {
            final TextView dates = findViewById(R.id.home_temperature_date);
            final String stringExtra = getIntent().getStringExtra(DATE);
            setText(dates, stringExtra);
        }
    }

    @Override
    public void initDataNet() {

        final RecyclerView recycler = findViewById(R.id.activity_home_temperature_list);
        final ChartView chart = findViewById(R.id.activity_home_temperature_chart);
        final boolean mulit = getIntent().getBooleanExtra(MULIT, false);
        getPresenter().temperatureList(this, recycler, chart, mulit);
    }

    @Override
    public void showDelete(int position) {

        final WarningDialog dialog = new WarningDialog(this);
        dialog.show();
        dialog.setTitles("确定删除体温");
        dialog.setButton("取消", "确定");
        dialog.setOnDialogChangeListener((left, right) -> {
            if (!right) return;
            final RecyclerView recycler = findViewById(R.id.activity_home_temperature_list);
            final ChartView chart = findViewById(R.id.activity_home_temperature_chart);
            getPresenter().deleteTemperature(TemperatureActivity.this, chart, recycler, position);
        });
    }

    @Override
    public void showTime() {

        final TimeDialog dialog = new TimeDialog(this);
        dialog.show();
        dialog.setTitles("选择时间");
        dialog.setOnTimeChangeListener((hour, minute) -> {
            final TextView text = findViewById(R.id.home_temperature_time);
            text.setText(hour + ":" + minute);
        });
    }

    @Override
    public void showCalendar(TextView text, int year, int month, int day) {

        final CalendarDialog dialog = new CalendarDialog(this);
        dialog.setScroll(getDate());
        dialog.show();
        dialog.setOnCalendarChangeListener((years, months, days, maxDay, isClick) -> {
            if (isClick) {
                final TextView date = findViewById(R.id.home_temperature_date);
                setText(date, year + "-" + months + "-" + days);
                final RecyclerView recycler = findViewById(R.id.activity_home_temperature_list);
                final ChartView chart = findViewById(R.id.activity_home_temperature_chart);
                getPresenter().temperatureList(this, recycler, chart, false);
            } else {
                getPresenter().schemelList(dialog, years, months, days, maxDay);
            }
        });
    }

    @Override
    public void showInput(String inputs, int position) {

        final InputDialog dialog = new InputDialog(this);
        dialog.show();
        dialog.setMenu(getResources().getString(R.string.home_temperature_menu));
        dialog.setTities(getResources().getString(R.string.home_temperature_name));
        dialog.setMax(45f);
        dialog.setMin(35f);
        dialog.setDotLength(1);
        dialog.setMaxLength(4);
        dialog.setInputType(8194);
        dialog.setInput(inputs);
        dialog.setOnDialogChangeListener(input -> {
            final RecyclerView recycler = findViewById(R.id.activity_home_temperature_list);
            final ChartView chart = findViewById(R.id.activity_home_temperature_chart);
            getPresenter().editTemperature(this, chart, recycler, input, position);
        });
    }

    @Override
    public String getDate() {
        final TextView text = findViewById(R.id.home_temperature_date);
        return text.getText().toString();
    }

    @Override
    public String getTime() {
        final TextView text = findViewById(R.id.home_temperature_time);
        return text.getText().toString();
    }

    @Override
    public String getTemperature() {
        final TextView text = findViewById(R.id.activity_home_temperature_input);
        return text.getText().toString();
    }
}